import {ResultCode} from "../../network.js";

export function getLoginUser(popup, backToIndex, user) {
    return axios.get("/user/now")
        .then(response => {
            const responseResult = response.data;
            if ((responseResult.code & ResultCode.FAIL) === ResultCode.FAIL) {
                popup.value.onConfirm = backToIndex;
                popup.value.onCancel = backToIndex;
                popup.value.show(responseResult.message);
            } else if (responseResult.code === ResultCode.SUCCESS) {
                user.value = responseResult.data;
            }
            return response;
        });
}

export function getLoginDoctor(popup, backToIndex, doctor) {
    return axios.get("/doctor/now")
        .then(response => {
            const responseResult = response.data;
            if ((responseResult.code & ResultCode.FAIL) === ResultCode.FAIL) {
                popup.value.onConfirm = backToIndex;
                popup.value.onCancel = backToIndex;
                popup.value.show(responseResult.message);
            } else if (responseResult.code === ResultCode.SUCCESS) {
                doctor.value = responseResult.data;
            }
            return response;
        });
}

export function getUserOrDoctor(popup, backToIndex, user) {
    const fakePopup = Vue.ref({
        onConfirm: '', onCancel: '', show(a) {
        }
    });
    return getLoginUser(fakePopup, '', user)
        .then(r => {
            if ((r.data.code & ResultCode.FAIL) === ResultCode.FAIL) {
                getLoginDoctor(popup, backToIndex, user).then(r => {
                    if ((r.data.code & ResultCode.FAIL) !== ResultCode.FAIL) {
                        user.value.type = "医生";
                    }
                });
            }
            user.value.type = "用户";
            return r;
        });
}

export function getHospitalFromId(id, hospital) {
    return axios.post('/hospital/id', {value: parseInt(id)})
        .then(response => {
            // console.log(response);
            const responseResult = response.data;
            if (responseResult.code === ResultCode.SUCCESS) {
                hospital.value = responseResult.data;
            }
            return response;
        });
}

export function getDepartmentsFromHospitalId(id, departments) {
    return axios.post('/department/fromHospital', {value: parseInt(id)})
        .then(response => {
            // console.log(response);
            const responseResult = response.data;
            if (responseResult.code === ResultCode.SUCCESS) {
                departments.value = responseResult.data;
            }
            return response;
        });
}

export function getAllRegister(hospitalId, departmentId, registerList) {
    let data = new URLSearchParams();
    // let data = new FormData();
    data.append("hospitalId", hospitalId);
    data.append("departmentId", departmentId);
    // console.log(data.toString());
    return axios.post("/register/all", data)
        // return axios.post("/register/all", {hospitalId, departmentId})
        .then(response => {
            console.log(response);
            const responseResult = response.data;
            if (responseResult.code === ResultCode.SUCCESS) {
                registerList.value[departmentId] = responseResult.data;
            }
            return response;
        }).catch(e => {
            console.log(e);
        });
}

export function addRegister(doctorId, userId, appointmentTimeType, appointmentDay
    , departmentId, hospitalId) {
    return axios.post("/register/add", {
        userId, doctorId, appointmentTimeType, appointmentDay,
        departmentId, hospitalId
    }).then(response => {
        console.log(response);
        return response;
    });
}

export function getWorkInfoFromId(hospitalId, workInfo) {
    return axios.post("/hospital/dp_work_info", {value: hospitalId})
        .then(response => {
            const responseResult = response.data;
            if (responseResult.code === ResultCode.SUCCESS) {
                workInfo.value = responseResult.data;
            }
            console.log(response);
            return response;
        });
}

export function getUserRegisterInfo(hospitalId, registerInfo) {
    return axios.post("/register/user_info", {value: hospitalId})
        .then(response => {
            const responseResult = response.data;
            if (responseResult.code === ResultCode.SUCCESS) {
                registerInfo.value = responseResult.data;
            }
            console.log(response);
            return response;
        });
}

export function getRegistersDoctorHave(registerList) {
    return axios.get("/register/doctor_have")
        .then(r => {
            const responseResult = r.data;
            if (responseResult.code === ResultCode.SUCCESS) {
                registerList.value = responseResult.data;
            }
            console.log(r);
            return r;
        })
}

export function caseFromRegister(registerId, caseInfo) {
    return axios.post("/case/from_register", {value: registerId})
        .then(r => {
            const responseResult = r.data;
            if (responseResult.code === ResultCode.SUCCESS) {
                caseInfo.value = responseResult.data;
            }
            console.log(r);
            return r;
        });
}

export function uploadCase(c, insIds, mdcIds, mdcCounts) {
    return axios.post("/case/upload", {
        case_: c,
        inspectionIds: Array.from(insIds),
        medicineIds: Array.from(mdcIds),
        medicineCounts: Array.from(mdcCounts)
    });
}

export function getAllTopics(searchInfo, pageNum, topicPage) {
    return axios.post("/topic/all_topics", {searchInfo, pageNum})
        .then(r => {
            if (r.data.code === ResultCode.SUCCESS) {
                topicPage.value = r.data.data;
            }
            console.log(r);
            return r;
        });
}

export function getTopicFromId(id, topic) {
    return axios.post("/topic/id", {value: id})
        .then(r => {
            if (r.data.code === ResultCode.SUCCESS) {
                topic.value = r.data.data;
            }
            console.log(r);
            return r;
        });
}

export function getAllReplies(topicId, pageNum, replyPage) {
    return axios.post("/topic/all_replies", {topicId, pageNum})
        .then(r => {
            if (r.data.code === ResultCode.SUCCESS) {
                replyPage.value = r.data.data;
            }
            console.log(r);
            return r;
        });
}